/**
 * 实体基类
 * @author zzhijie
 * @date 2021-04-30
 */
export class BasicEntity {
  /**
   * 拷贝对象属性
   * @returns {BasicEntity}
   */
  copy() {
    let instance = new this.constructor();
    for (let key in this) {
      if (this.hasOwnProperty(key)) {
        instance[key] = this[key];
      }
    }
    return instance;
  }

  /**
   * 类型转换
   * @param obj {Object} 目标对象
   * @param ownProperty? {boolean} 只拷贝自身存在的属性
   * @returns {BasicEntity}
   */
  static convert(obj, ownProperty) {
    let instance = new this();
    for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        if (ownProperty) {
          if (instance.hasOwnProperty(key)) {
            instance[key] = obj[key];
          }
        } else {
          instance[key] = obj[key];
        }
      }
    }
    return instance;
  }
}
